package sk.fiit.icp.data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Event extends Events {
	private Event parent;
	private List<Event> childrens = new ArrayList<Event>();
	private Events eventDB;

	public Event(Events eventDB, Event parent, List<Event> childrens) {
		this.eventDB = eventDB;
		if ( eventDB != null ) {

			setAllDayEvent( eventDB.getAllDayEvent() );
			setArchive( eventDB.getArchive() );
			setEndTime( eventDB.getEndTime() );
			setEventCategories( eventDB.getEventCategories() );
			setEvents( eventDB.getEvents() );
			setEventses( eventDB.getEventses() );
			setIdevent( eventDB.getIdevent() );
			setLocation( eventDB.getLocation() );
			setPriority( eventDB.getPriority() );
			setProgress( eventDB.getProgress() );
			setRepetition( eventDB.getRepetition() );
			setStartTime( eventDB.getStartTime() );
			setText( eventDB.getText() );
			setTitle( eventDB.getTitle() );
			setUsers( eventDB.getUsers() );
			setParent( parent );
			setChildrens( childrens );
		}
	}

	public void remove() {
		if ( parent != null ) {
			parent.childrens.remove( this );
			parent = null;
		}
	}

	public int indexOf(Event drop) {
		return childrens.indexOf( drop );

	}

	public void add(Event drag, int indexOf) {
		drag.remove();
		drag.parent = this;
		childrens.add( indexOf, drag );

	}

	public void add(Event drag) {
		add( drag, childrens.size() );

	}

	public boolean hasChildren() {
		return !childrens.isEmpty();
	}

	public List<Event> getChildrens() {
		return this.childrens;
	}

	public boolean isAncestor(Object event) {
		if ( parent == event ) {
			return true;
		}

		if ( parent == null ) {
			return false;
		} else {
			return parent.isAncestor( event );
		}
	}

	public void setParent(Event parent) {
		this.parent = parent;
	}

	public void setChildrens(List<Event> childrens) {
		if ( childrens == null ) {
			this.childrens = new ArrayList<Event>();
		} else {
			this.childrens = childrens;
		}
	}

	@Override
	public String toString() {
		return super.getTitle();
	}

	public Event getParent() {
		return parent;
	}

	public Event copy() {
		Event copy = new Event( this.eventDB, parent, new LinkedList<Event>() );

		for ( Event child : childrens ) {
			copy.add( child.copy() );
		}

		return copy;
	}

	public Events getEventDB() {
		return eventDB;
	}

	@Override
	public boolean equals(Object o) {
		if ( o != null && getIdevent() == ((Event) o).getIdevent() && getTitle().equals( ((Event) o).getTitle() ) ) {
			return true;
		}
		return false;
	}
}
